
class Slider {
  load({ dom, leftFn, rightFn, upFn, downFn }) {
    //判定是否为手持设备
    var sUserAgent = navigator.userAgent;
    console.log('设备:', sUserAgent)
    if (sUserAgent.indexOf('Android') > -1 || sUserAgent.indexOf('iPhone') > -1 || sUserAgent.indexOf('iPad') > -1 || sUserAgent.indexOf('iPod') > -1 || sUserAgent.indexOf('Symbian') > -1 || sUserAgent.indexOf('zscc-web') > -1) {
      this.touch = true
    } else {
      this.touch = false
    }
    this.StartEvent = this.touch ? "touchstart" : "mousedown"; //支持触摸式使用相应的事件替代
    this.MoveEvent = this.touch ? "touchmove" : "mousemove";
    this.EndEvent = this.touch ? "touchend" : "mouseup";
    this.dom = dom // 触摸事件的div
    this.domWidth = 375 // 触摸滑动的div宽度
    this.leftFn = leftFn
    this.rightFn = rightFn
    this.upFn = upFn
    this.downFn = downFn
    this.x = []; //记录鼠标移动是的X坐标
    this.y = []; //记录鼠标移动是的Y坐标
    
    this.lock = false; //鼠标移动前，判断鼠标是否按下
    this.bind()
  }
  bind() {
    var that = this
    this.dom["on" + that.StartEvent] = function(e) {
      that.lock = true;
      that.domWidth = that.dom.style.width || that.dom.clientWidth || that.dom.offsetWidth || that.dom.scrollWidth
      // e.preventDefault();
    }
    this.dom["on" + that.MoveEvent] = function(e) {
      var touch = that.touch ? e.touches[0] : e;
      if (that.lock) {
        var touch = that.touch ? e.touches[0] : e;
        that.x.push(touch.clientX)
        that.y.push(touch.clientY)
      }
      // e.preventDefault();
    }
    this.dom["on" + that.EndEvent] = function(e) {
      if (that.x.length > 0) {
        
        let v = that.x[that.x.length - 1] - that.x[0]
        if (Math.abs(v) >= 50) {
          console.log(v > 0 ? '往右' : '往左')
          if (v > 0) {
            that.rightFn?.()
          } else {
            that.leftFn?.()
          }
        }
      }
      if (that.y.length > 0) {
        let v = that.y[that.y.length - 1] - that.y[0]
        if (Math.abs(v) >= 50) {
          console.log(v > 0 ? '往下' : '往上')
          if (v > 0) {
            that.downFn?.()
          } else {
            that.upFn?.()
          }
        }
      }

      that.x = [];
      that.y = [];
      that.lock = false;
      // e.preventDefault();
    }
  }
  destroy() {
    this.dom["on" + this.StartEvent] = null
    this.dom["on" + this.MoveEvent] = null
    this.dom["on" + this.EndEvent] = null
    console.log('destroy')
  }
}
exports.Slider = Slider